package presstrack.targets
{
	import mx.collections.ArrayCollection;	
	import org.presstrack.service.CrudService;
	
	public class PressTargetClass extends TargetClass
	{
		public var mPressLabel	:String;
		public var mPressRun	:String;
		public var mPressSheet	:String;
		public var mPressColor	:String;
		public var mPaperLabel	:String;
		public var mPaperThick	:String;	
		
		public var arTargets	:ArrayCollection;
		
		// static
		
		public static var cur_date				:Date	= new Date();			
		public static var PRESS_PLAN_START_DATE	:Date	= new Date(cur_date.fullYear, cur_date.month+1, cur_date.date);
		public static var PRESS_PLAN_END_DATE	:Date	= new Date(cur_date.fullYear, cur_date.month+1, cur_date.date);
		
		public static var TARGET_ORDERID	:int		= new int(-1);
		public static var TARGET_CUSTOMER	:String 	= new String();
		public static var TARGET_PRESS		:String 	= new String();		
			
		
		public function PressTargetClass(ob:Object = null)
		{
			if (ob != null)
			{
				copyFrom(ob);
			}						
		}
		
		override public function copyFrom(ob:Object):void 
		{
			super.copyFrom(ob);
			
			if (ob.hasOwnProperty('mPressLabel'))
			{
				mPressLabel = ob.mPressLabel;
			}
				
			if (ob.hasOwnProperty('mPressRun'))
			{
				mPressRun = ob.mPressRun;
			}
				
			if (ob.hasOwnProperty('mPressSheet'))
			{
				mPressSheet = ob.mPressSheet;
			}
				
			if (ob.hasOwnProperty('mPressColor'))
			{
				mPressColor = ob.mPressColor;
			}
				
			if (ob.hasOwnProperty('mPaperLabel'))
			{
				mPaperLabel = ob.mPaperLabel;	
			}		
				
			if (ob.hasOwnProperty('mPaperThick'))
			{
				mPaperThick = ob.mPaperThick;
			}
				
			if(ob.hasOwnProperty('arTargets'))
			{
				arTargets = new ArrayCollection();
				for each (var trg:Object in ob.arTargets)
				{
					var target:TargetClass = new TargetClass(trg);
					arTargets.addItem(target);
				}
			}			
		}
		
		override protected function setStartDate():Boolean 
		{
			super.setStartDate();
			
			if (obStartDate == null)
			{
				return false;
			}
			
			if (PressTargetClass.PRESS_PLAN_START_DATE == null
			 || obStartDate.getTime() < PressTargetClass.PRESS_PLAN_START_DATE.getTime())
			{
				PressTargetClass.PRESS_PLAN_START_DATE = new Date(obStartDate.fullYear, obStartDate.month, obStartDate.date);
			}			
			
			if (PressTargetClass.PRESS_PLAN_END_DATE == null 
			 || (obStartDate.getTime() + iTimeLength * CrudService.MILLISECONDS_PER_MINUTE) > PressTargetClass.PRESS_PLAN_END_DATE.getTime())	
			{
				PressTargetClass.PRESS_PLAN_END_DATE = new Date(obStartDate.fullYear, obStartDate.month, obStartDate.date);
				PressTargetClass.PRESS_PLAN_END_DATE.setTime(PressTargetClass.PRESS_PLAN_END_DATE.getTime() + iTimeLength * CrudService.MILLISECONDS_PER_MINUTE);
				PressTargetClass.PRESS_PLAN_END_DATE.setTime(PressTargetClass.PRESS_PLAN_END_DATE.getTime() + CrudService.MILLISECONDS_PER_DAY);
			}
			
			CrudService.arPlanPress.refresh();
			
			var temp_start_time:Date = new Date(obStartDate);
			for (var i:int = arTargets.length - 1; i >= 0; --i)
			{
				if (arTargets[i].iTaskSection < 4)
				{
					if (arTargets[i].mStartDate == "-/-") 
					{
						temp_start_time.setTime(temp_start_time.getTime() - (arTargets[i].iTimeLength * CrudService.MILLISECONDS_PER_MINUTE));
						arTargets[i].start_date = new Date(temp_start_time);						
					}
				}
			}
				
			temp_start_time = new Date(obStartDate);
			temp_start_time.setTime(temp_start_time.getTime() + (iTimeLength * CrudService.MILLISECONDS_PER_MINUTE));
			for (i = 0; i < arTargets.length; ++i)
			{
				if (arTargets[i].iTaskSection > 4)
				{
					if (arTargets[i].mStartDate == "-/-")
					{
						arTargets[i].mStartDate = new Date(temp_start_time);	
						temp_start_time.setTime(temp_start_time.getTime() + (arTargets[i].iTimeLength * CrudService.MILLISECONDS_PER_MINUTE));											
					}
				}
			}
			return true;
		} 
	}
}